package com.hertz.alarm.interceptor;

import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpHead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hertz.alarm.controller.support.ResultModel;
import com.hertz.alarm.entity.IrisUser;
import com.hertz.alarm.service.IrisUserService;
import com.hertz.alarm.util.Constants4HTTP;



public class ApiTokenInterceptor extends HandlerInterceptorAdapter{
		private Logger logger = LoggerFactory.getLogger(ApiTokenInterceptor.class);
		
		@Resource
		private IrisUserService irisUserService;
		@Resource
		private MessageSource messageSource;
//		@Resource
//		private TIdolService idolService;
	 
		public boolean preHandle(HttpServletRequest request, 
			HttpServletResponse response, Object handler)
		    throws Exception {
			//boolean login = false;
			String openidToken = request.getHeader(Constants4HTTP.HEADER_OPENID_TOKEN);
			if(StringUtils.isBlank(openidToken)){
				response.setStatus(HttpServletResponse.SC_FORBIDDEN);
				return false;
			}else{
				IrisUser irisUser = irisUserService.getByToken(openidToken);
				if(irisUser == null){
					response.setStatus(HttpServletResponse.SC_FORBIDDEN);
					return false;
				}
				request.setAttribute(Constants4HTTP.REQ_ATTR_USER, irisUser);
			}
			return true;
		}

}